<?php

/****************************	BEGIN: PERMISSION HELPER ***************************/
/**
 * admin_check_permission()
 */
if (!function_exists('admin_check_permission')) {

	function admin_check_permission($module_id,$permission_type,$allow_exit=false) {
		$result = true;
		/*$result = false;
		if(!empty($_SESSION['user_is_root'])){
			$result = true;
		} else if(!empty($_SESSION['user_permission'])){
			$permission = $_SESSION['user_permission'];//pr('$permission='.$permission);
			$normalized_module_id = admin_normalize_module_id($module_id);//pr('$normalized_module_id='.$normalized_module_id);
			$pos = strpos($permission,$normalized_module_id);
			$sub_str = substr($permission,$pos+4,3);//pr('$sub_str='.$sub_str,1);
			$pos_result = strpos($sub_str,$permission_type);
			$result = $pos_result !== false;
		}
		if(!$result && $allow_exit){
			if(IS_AJAX){
				echo 'permission-denied';exit();
			} else {
				header('Location: '.PATH_URL.'admincp/permission');
				exit();
			}
		}*/
		return $result;
	}
}

/**
 * admin_normalize_module_id()
 */
if (!function_exists('admin_normalize_module_id')) {
	function admin_normalize_module_id($id){
		return str_pad($id, 3, "0", STR_PAD_LEFT);
	}
}

/**
 * admin_check_permission_unit()
 * Ex: 004|rwd
 */
if (!function_exists('admin_check_permission_unit')) {
	function admin_check_permission_unit($permission,$offset,$permission_unit){
		$result = false;
		$pos = 0;
		$length = strlen($permission_unit);
		switch($permission_unit){
			case '---': // No access
			case 'r': // Read
				$pos = 4;
				break;
			case 'w': // Write
				$pos = 5;
				break;
			case 'd': // Delete
				$pos = 6;
				break;
		}
		if(!empty($permission)){
			if(!empty($pos)){
				$segment = substr($permission,$offset+$pos,$length);
				$result = $segment== $permission_unit;
			}
		} else {
			$result = $permission_unit == '---'; // If there is no permission, default is No access
		}
		return $result;
	}
}

/****************************	END: PERMISSION HELPER ***************************/

/**
 * admin_get_cell_value()
 */
if (!function_exists('admin_get_cell_value')) {

	function admin_get_cell_value($obj,$module,$field_config,$controller) {
		$CI = & get_instance();
		$key = $field_config['key'];
		$field_value = !empty($obj->$key) ? $obj->$key : '';
		if(!empty($field_config['file_config'])){
			$file_config = $field_config['file_config'];
			$file_type = $file_config['file_type'];
			$file_dir_path = $file_config['file_dir_path'];
			switch($file_type){
				case 'image':
					if(!empty($field_value)){
						$file_config['src'] = PATH_URL.$file_dir_path.$field_value;
					} else {
						$file_config['src'] = PATH_URL.'static/images/no_image.jpg';
					}
					// Override width, height
					$file_config['file_width'] = !empty($field_config['file_width']) ? $field_config['file_width'] : (!empty($file_config['file_width']) ? $file_config['file_width'] : 0);
					$file_config['file_height'] = !empty($field_config['file_height']) ? $field_config['file_height'] : (!empty($file_config['file_height']) ? $file_config['file_height'] : 0);
					$value = $CI->load->view('BACKEND/list_view/images', $file_config, true);
					break;
				case 'audio':
					if(!empty($field_value)){
						$file_config['src'] = PATH_URL.$file_dir_path.$field_value;
					} else {
						$file_config['src'] = '';
					}
					$value = $controller->load->view('BACKEND/list_view/audio_player',$file_config, true);
					break;
				case 'video':
					if(!empty($field_value)){
						$file_config['src'] = PATH_URL.$file_dir_path.$field_value;
					} else {
						$file_config['src'] = '';
					}
					$value = $controller->load->view('BACKEND/list_view/video_player',$file_config, true);
					break;
			}
		} else if(!empty($field_config['datetime_type'])){
			$datetime_value = $field_value;
			switch($field_config['datetime_type']){
				case 'datetime':
					$value = getVNDateTime($datetime_value,array('show_time_second'=>false));
					break;
				case 'date':
					$value = getVNDateTime($datetime_value,array('show_time'=>false));
					break;
				case 'time':
					$value = getVNDateTime($datetime_value,array('show_date'=>false,'show_time'=>true,'show_time_second'=>false));
					break;
			}
		} else if(!empty($field_config['select_config'])){
			$select_config = $field_config['select_config'];
			if(!empty($select_config['value'])){
				$option_list = $select_config['value'];
				$key_val = $field_value;
				if(!empty($option_list[$key_val])){
					$value = $option_list[$key_val];
				}
			} else if(!empty($select_config['referred_field_name'])){
				$referred_field_name = $select_config['referred_field_name'];
				$referred_select_field_name = $key.'_'.$referred_field_name;
				$value = $obj->$referred_select_field_name;
			}
		} else {
			$value = $field_value;
		}
		
		if(!empty($field_config['func']))
		{
			$func = $field_config['func'];
			$value = $controller->$func($field_value,$obj);
		}
		
		return !empty($value) ? $value : '';
	}
}

/**
 * admin_get_detail_url()
 */
if (!function_exists('admin_get_detail_url')) {

	function admin_get_detail_url($obj,$module) {
		$url = PATH_URL.'admincp/'.$module.'/update/'.$obj->id;
		return $url;
	}
}

/**
 * admin_get_detail_link()
 */
if (!function_exists('admin_get_detail_link')) {

	function admin_get_detail_link($obj,$module,$field_config,$controller) {
		$url = admin_get_detail_url($obj,$module);
		$value = admin_get_cell_value($obj,$module,$field_config,$controller);
		$html = "<a href='{$url}'>{$value}</a>";
		return $html;
	}
}

/**
 * admin_get_list_view_cell_html()
 */
if (!function_exists('admin_get_list_view_cell_html')) {

	function admin_get_list_view_cell_html($obj,$module,$field_config,$controller) {
		$html = '';
		if(!empty($field_config['is_show_link'])){
			$html = admin_get_detail_link($obj,$module,$field_config,$controller);
		} else {
			$html = admin_get_cell_value($obj,$module,$field_config,$controller);
		}
		return $html;
	}
}

/**
 * admin_get_multi_level_select()
 *
 * @param list data
 * @return multi level option tags for select
 */
if (!function_exists('admin_get_multi_level_select')) {
	function admin_get_multi_level_select($parent_list = '', $parent_id = 0, $parent_id_field_name, $parent_title_field_name, $prefix = '', $delimeter = '--') {
		$select='';
		if(!empty($parent_id)){
			$prefix .= $delimeter;
		}
		foreach ($parent_list as $v) {
			
			if($v->$parent_id_field_name == $parent_id){
				$option_value = $v->id;
				$option_text = $prefix.$v->$parent_title_field_name;
				$select .= "<option value='{$option_value}'>{$option_text}</option>";
				$select .= admin_get_multi_level_select($parent_list, $v->id, $parent_id_field_name, $parent_title_field_name, $prefix);
			}
		}
		return $select;
	}
}

/**
 * admin_get_select_options()
 *
 * @param list data
 * @return option tags for select
 */
if (!function_exists('admin_get_select_options')) {

	function admin_get_select_options($list_data = '',  $parent_title_field_name='title', $col_id = 'id') {
		$select='';
		foreach ($list_data as $v) {
			$select .= "<option value=\"".$v->$col_id."\">".$v->$parent_title_field_name."</option>";
		}
		return $select;
	}    

}

/**
 * admin_build_sub_select_config()
 */
if (!function_exists('admin_build_sub_select_config')) {

	function admin_build_sub_select_config($sub_select_config,$parent_id) {
		$select_config = $sub_select_config;
		$select_config['referred_condition'] = $sub_select_config['referred_condition'] . " = " . $parent_id;
		$data['select_config'] = $select_config;
		$data['text'] = $sub_select_config['text'];
		$data['type'] = 'select';
		$data['key'] = $sub_select_config['name'];
		$data['name'] = $sub_select_config['name'];
		return $data;
	}    

}